import collections
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
total = n
min_removals = total
previous = 0
c = collections.Counter(collections.Counter(arr).values())
current_count = sum(c.values())
for k in sorted(c.keys()):
current_cost = total - (current_count * k)
min_removals = min(min_removals, current_cost)
current_count -= c[k]
previous += (c[k] * k)
print(min_removals)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int tc;
cin>>tc;
while(tc--)
{
int n,x;
cin>>n;
vector<int>v;
map<int,int>freq,umap;
for(int i=0;i<n;i++)
{
cin>>x;
freq[x]++;
}
for(auto it:freq)
{
int y=it.second;
umap[y]++;
if(umap[y]==1)
v.push_back(y);
}
sort(v.rbegin(),v.rend());
vector<int>suffix(v.size());
suffix[v.size()-1]=0;
for(int i=v.size()-2;i>=0;i--)
suffix[i]=suffix[i+1]+(v[i+1]*umap[v[i+1]]);
int mini=suffix[0],carry=umap[v[0]],add=0;
for(int i=1;i<v.size();i++)
{
add+=((v[i-1]-v[i])*carry);
mini=min(mini,add+suffix[i]);
carry+=umap[v[i]];
}
cout<<mini<<endl;
}
}
74. Search a 2D Matrix | 71. Simplify Path |
62. Unique Paths | 50. Pow(x, n) |
43. Multiply Strings | 34. Find First and Last Position of Element in Sorted Array |
33. Search in Rotated Sorted Array | 17. Letter Combinations of a Phone Number |
5. Longest Palindromic Substring | 3. Longest Substring Without Repeating Characters |
1312. Minimum Insertion Steps to Make a String Palindrome | 1092. Shortest Common Supersequence |
1044. Longest Duplicate Substring | 1032. Stream of Characters |
987. Vertical Order Traversal of a Binary Tree | 952. Largest Component Size by Common Factor |
212. Word Search II | 174. Dungeon Game |
127. Word Ladder | 123. Best Time to Buy and Sell Stock III |
85. Maximal Rectangle | 84. Largest Rectangle in Histogram |
60. Permutation Sequence | 42. Trapping Rain Water |
32. Longest Valid Parentheses | Cutting a material |
Bubble Sort | Number of triangles |
AND path in a binary tree | Factorial equations |